package bbk.nfc.widget;

import java.util.LinkedList;
import java.util.Queue;

import bbk.nfc.R;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class NfcToast extends Toast
{
	
	private TextView mText;
	static Activity mActivity;
	public static Queue < String > mQueue = new LinkedList < String >();
	private final static Handler mHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			case 0:
				String s = mQueue.poll();
				if (s != null)
				{
					ViewGroup vg = (ViewGroup) mActivity.findViewById(R.id.toast_layout_root);
					NfcToast toast = new NfcToast(mActivity, R.layout.toast, vg);
					toast.mText.setText(s);
					toast.show();
					sendEmptyMessageDelayed(0, 50);
				}
				break;
			default:
				break;
			}
			
		}
	};
	
	private NfcToast(Context context, int id, ViewGroup vg)
	{
		super(context);
		setDuration(Toast.LENGTH_SHORT);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(id, vg);
		setView(view);
		setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
		mText = (TextView) view.findViewById(R.id.text);
		
		
	}
	
	public static void showToast(Activity activity, String s)
	{
		mActivity = activity;
		mQueue.offer(s);
		if (mQueue.size() == 1)
		{
			mHandler.sendEmptyMessageDelayed(0, 0);
		}
	}
}
